//
//  FitpeopleViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/5.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FitpeopleViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "FoodCell.h"
#import "Food.h"
#import "FoodDatilViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
@interface FitpeopleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)UITableView *fvcTable;
@property(nonatomic,retain)NSMutableArray *fvcArr;
@property(nonatomic,assign)NSInteger page;
@end

@implementation FitpeopleViewController
- (void)dealloc
{
    [_fvcArr release];
    [_fvcTable release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.fvcArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    [self creatFvcView];
    [self PleaseData];
    [self pulldow];
//    [self pullup];
//    [self method];
}
//试图创建
-(void)creatFvcView
{
    self.fvcTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.fvcTable];
    self.fvcTable.delegate = self;
    self.fvcTable.dataSource = self;
    self.fvcTable.rowHeight = 120;
    self.fvcTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_fvcTable release];
}
//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fvcArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[FoodCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    Food * f = self.fvcArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",f.img];
    NSURL *url = [NSURL URLWithString:str];
    [cell.picView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.tagLabel.text = f.tag;
    cell.nameLabel.text = f.name;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//数据请求
-(void)PleaseData
{
//    NSString *str = @"http://api.yi18.net/cook/list?id=27";
//    NSString *str = @"http://api.yi18.net/cook/list?page=1&limit=20&id=27";
    NSString *str = [NSString stringWithFormat:@"http://api.yi18.net/cook/list?page=%ld&limit=20&id=27",self.page];
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *tep in arr) {
            Food *f = [[Food alloc]init];
            [f setValuesForKeysWithDictionary:tep];
            [self.fvcArr addObject:f];
            [f release];
        }
        for (int i = 0; i < self.fvcArr.count; i++) {
            if ([self.fvcArr[i] img] == nil) {
                [self.fvcArr removeObjectAtIndex:i];
            }
        }
        [self.fvcTable reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
    }];
    self.page++;
}
//加载刷新
- (void)method
{
    [self.fvcTable addLegendHeaderWithRefreshingBlock:^{
        [self.fvcArr removeAllObjects];
        self.page = 1;
        [self PleaseData];
        [self.fvcTable reloadData];
        [self.fvcTable.header endRefreshing];
    }];
    [self.fvcTable.legendHeader beginRefreshing];
    [self.fvcTable addLegendFooterWithRefreshingBlock:^{
        self.page++;
        [self PleaseData];
        [self.fvcTable reloadData];
        [self.fvcTable.footer endRefreshing];
    }];
}

//下拉刷新
-(void)pulldow
{
    [self.fvcTable addLegendHeaderWithRefreshingBlock:^{
        [self.fvcArr removeAllObjects];
        self.page = 1;
        [self PleaseData];
        [self.fvcTable reloadData];
        [self.fvcTable.header endRefreshing];
    }];
//    [self.fvcTable addLegendHeaderWithRefreshingBlock:^{
//        [self PleaseData];
//        [self.fvcTable reloadData];
//        [self.fvcTable.header endRefreshing];
//    }];
}
//上拉刷新
-(void)pullup
{
//    [self.fvcTable addLegendFooterWithRefreshingBlock:^{
//        [self PleaseData];
//        [self.fvcTable reloadData];
//        [self .fvcTable.footer endRefreshing];
//        
//    }];
    
    
}
//跳转详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDatilViewController *fvc = [[FoodDatilViewController alloc]init];
    Food *food = self.fvcArr[indexPath.row];
    fvc.id = food.id
    ;
    fvc.title = food.name;
    [self.navigationController pushViewController:fvc animated:YES];
    [fvc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
